package com.cnpc.ds.annotation;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class ExceptionMonitorAspect {

    private final Logger log = LoggerFactory.getLogger(getClass());

    // 定义切入点表达式，这里匹配所有使用 @ExceptionMonitor 注解的方法
    @Pointcut("@annotation(com.cnpc.ds.annotation.ExceptionMonitor)")
    public void monitor() {

    }

    // 使用 @Around 注解来包裹方法调用，在方法执行前后插入逻辑
    @Around("monitor()")
    public Object logExecution(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        try {
            return joinPoint.proceed();
        } catch (Exception e) {
            log.error("方法{}--->异常: {}",signature.getMethod().getName(),e.getMessage());
            return null;
        }
    }
}
